package campus.service.impl;

import campus.mapper.CredentialsLogMapper;
import campus.mapper.FinancialMapper;
import campus.mapper.SubjectsMapper;
import campus.model.CredentialsLog;
import campus.model.CredentialsLogSchedule;
import campus.model.Subjects;
import campus.service.CredentialsLogService;
import campus.util.ConvertNumber;
import campus.util.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ：wang
 * @description：TODO
 * @date ：2020/5/9 22:30
 */
@Service
public class CredentialsLogServiceImpl implements CredentialsLogService{
    @Autowired
    private CredentialsLogMapper credentialsLogMapper;
    @Autowired
    private SubjectsMapper subjectsMapper;
    @Autowired
    private FinancialMapper financialMapper;
    /*
     * @Author: wang
     * @Description:添加
     * @Date: 22:31 2020/5/9
     * @param: credentialsLog
     * @return: void
     */
    @Override
    public void insert(CredentialsLog credentialsLog) throws ParseException {
        CredentialsLog c1 = new CredentialsLog();
        CredentialsLog c2 = new CredentialsLog();
        String[] s1 = credentialsLog.getKm().split(",");
        String[] s2 = credentialsLog.getJzAbstract().split(",");
        c1.setJzKmId(Integer.parseInt(s1[0]));
        c2.setJzKmId(Integer.parseInt(s1[1]));
        c1.setJzAbstract(s2[0]);
        c2.setJzAbstract(s2[1]);
        c1.setJzProof(credentialsLog.getJzProof());
        c2.setJzProof(credentialsLog.getJzProof());
        c1.setJzH(credentialsLog.getJzH());
        c2.setJzH(credentialsLog.getJzH());
        c1.setJzDate(new SimpleDateFormat("yyyy-MM-dd").parse(credentialsLog.getDatea()));
        c2.setJzDate(new SimpleDateFormat("yyyy-MM-dd").parse(credentialsLog.getDatea()));
        Date date = new Date();
        c1.setJzCreatedate(date);
        c2.setJzCreatedate(date);
        if (credentialsLog.getJzJfprice().indexOf(",")==-1){
            c1.setJzJfprice(credentialsLog.getJzJfprice());
            c1.setJzDfprice("");
        }else{
            c2.setJzJfprice(credentialsLog.getJzJfprice().
                    substring(credentialsLog.getJzJfprice().indexOf(",")+1,credentialsLog.getJzJfprice().length()));
            c2.setJzDfprice("");
        }
        if (credentialsLog.getJzDfprice().indexOf(",")==-1){
            c1.setJzDfprice(credentialsLog.getJzDfprice());
            c1.setJzJfprice("");
        }else{
            c2.setJzDfprice(credentialsLog.getJzDfprice().
                    substring(credentialsLog.getJzDfprice().indexOf(",")+1,credentialsLog.getJzDfprice().length()));
            c2.setJzJfprice("");
        }
        //System.err.println(c1);
        //System.err.println(c2);
        credentialsLogMapper.insert(c1);
        credentialsLogMapper.insert(c2);
        updateGx();
    }
    @Override
    public void insert1(CredentialsLog credentialsLog) {
        if (!credentialsLog.getJzAbstract().equals("")){
            credentialsLogMapper.insert(credentialsLog);
        }
    }
    @Override
    public List<CredentialsLog> selectByExample() {
        return credentialsLogMapper.selectByExample(null);
    }

    @Override
    public PageResult selectByAllPageSize(CredentialsLogSchedule record, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<CredentialsLog> findlog = (Page<CredentialsLog>) credentialsLogMapper.selectByAllPageSize(record);
        List<CredentialsLog> credentialsLogs = findlog.getResult();

        ArrayList<CredentialsLogSchedule> list = new ArrayList();  //存单个表
        ArrayList<CredentialsLogSchedule> list2 = new ArrayList(); //存双个表
        ArrayList<CredentialsLogSchedule> list3 = new ArrayList(); //存 sum
        for (CredentialsLog log : credentialsLogs){
            CredentialsLogSchedule schedule = new CredentialsLogSchedule();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM
            String format = simpleDateFormat.format(log.getJzDate());
            String format2 = null;
            CredentialsLogSchedule schedule1 = new CredentialsLogSchedule();
            for (CredentialsLogSchedule schedule2 : list){
                BeanUtils.copyProperties(schedule2, schedule1);
                if(schedule2.getJzDate() != null){
                    SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM
                     format2 = simpleDateFormat2.format(schedule2.getJzDate());
                }
            }
            if(format.equals(format2)){
                format2 = null;
                schedule1.setJzAbstract2(log.getJzAbstract());
                schedule1.setJzCreatedate2(log.getJzCreatedate());
                schedule1.setJzDate2(log.getJzDate());
                schedule1.setJzDfprice2(log.getJzDfprice());
                schedule1.setJzH2(log.getJzH());
                schedule1.setJzId2(log.getJzId());
                schedule1.setJzKmId2(log.getJzKmId());
                schedule1.setJzJfprice2(log.getJzJfprice());
                schedule1.setJzProof2(log.getJzProof());
                schedule1.setJzDfprice2(log.getJzDfprice());
                schedule1.setKm2(log.getKm());
                list2.add(schedule1);

                for(CredentialsLogSchedule schedule2 : list2){
                    int df , df2 , jf , jf2 ;
                    if(schedule2.getJzDfprice() == null || schedule2.getJzDfprice().equals("")){
                        df = 0;
                    }else{
                        df =  Integer.parseInt(schedule2.getJzDfprice());
                    }
                    if(schedule2.getJzDfprice2() == null || schedule2.getJzDfprice2().equals("")){
                        df2 = 0;
                    }else{
                        df2 =  Integer.parseInt(schedule2.getJzDfprice2());
                    }
                    if(schedule2.getJzJfprice() == null || schedule2.getJzJfprice().equals("")){
                        jf = 0;
                    }else{
                        jf =  Integer.parseInt(schedule2.getJzJfprice());
                    }
                    if(schedule2.getJzJfprice2() == null || schedule2.getJzJfprice2().equals("")){
                        jf2 = 0;
                    }else{
                        jf2 =  Integer.parseInt(schedule2.getJzJfprice2());
                    }
                    int sumdf = (df + df2);
                    int sumjf = (jf + jf2);
                    schedule1.setDfsum(sumdf+"");
                    schedule1.setJfsum(sumjf+"");
                    schedule1.setConvert(ConvertNumber.numberToChinese(sumjf)+"元");
                    list3.add(schedule1);
                }
                list2.remove(0);
            }else{
                BeanUtils.copyProperties(log, schedule);
                list.add(schedule);
            }
        }
        long total = findlog.getTotal()/2;
        return new PageResult(total,list3);
    }

    @Override
    public void delete(CredentialsLogSchedule record) {
        credentialsLogMapper.deleteByPrimaryKey(record.getJzId());
        credentialsLogMapper.deleteByPrimaryKey(record.getJzId2());
    }

    @Override
    public void pldelete(Integer id) {
        credentialsLogMapper.deleteByPrimaryKey(id);
    }

    @Override
    public CredentialsLogSchedule selectById(Integer jzId, Integer jzId2) {
        CredentialsLogSchedule schedule = credentialsLogMapper.selectById(jzId);
        CredentialsLogSchedule schedule2 = credentialsLogMapper.selectById(jzId2);
        schedule.setJzAbstract2(schedule2.getJzAbstract());
        schedule.setJzCreatedate2(schedule2.getJzCreatedate());
        schedule.setJzDate2(schedule2.getJzDate());
        schedule.setJzDfprice2(schedule2.getJzDfprice());
        schedule.setJzH2(schedule2.getJzH());
        schedule.setJzId2(schedule2.getJzId());
        schedule.setJzKmId2(schedule2.getJzKmId());
        schedule.setJzJfprice2(schedule2.getJzJfprice());
        schedule.setJzProof2(schedule2.getJzProof());
        schedule.setJzDfprice2(schedule2.getJzDfprice());
        schedule.setKm2(schedule2.getKm());

        int df , df2 , jf , jf2 ;
        if(schedule.getJzDfprice() == null || schedule.getJzDfprice().equals("")){
            df = 0;
        }else{
            df =  Integer.parseInt(schedule.getJzDfprice());
        }
        if(schedule.getJzDfprice2() == null || schedule.getJzDfprice2().equals("")){
            df2 = 0;
        }else{
            df2 =  Integer.parseInt(schedule.getJzDfprice2());
        }
        if(schedule.getJzJfprice() == null || schedule.getJzJfprice().equals("")){
            jf = 0;
        }else{
            jf =  Integer.parseInt(schedule.getJzJfprice());
        }
        if(schedule.getJzJfprice2() == null || schedule.getJzJfprice2().equals("")){
            jf2 = 0;
        }else{
            jf2 =  Integer.parseInt(schedule.getJzJfprice2());
        }
        int sumdf = (df + df2);
        int sumjf = (jf + jf2);
        schedule.setDfsum(sumdf+"");
        schedule.setJfsum(sumjf+"");
        schedule.setConvert(ConvertNumber.numberToChinese(sumjf)+"元");

        return schedule;
    }

    @Override
    public void update(CredentialsLogSchedule record) {
        CredentialsLog log = new CredentialsLog();
        BeanUtils.copyProperties(record, log);
        CredentialsLog log2 = new CredentialsLog();

        log2.setJzAbstract(record.getJzAbstract2());
        log2.setJzCreatedate(record.getJzCreatedate2());
        log2.setJzDate(record.getJzDate2());
        log2.setJzDfprice(record.getJzDfprice2());
        log2.setJzH(record.getJzH2());
        log2.setJzId(record.getJzId2());
        log2.setJzKmId(record.getJzKmId2());
        log2.setJzJfprice(record.getJzJfprice2());
        log2.setJzProof(record.getJzProof2());
        log2.setJzDfprice(record.getJzDfprice2());
        log2.setKm(record.getKm2());
        credentialsLogMapper.updateByPrimaryKeySelective(log);
        credentialsLogMapper.updateByPrimaryKeySelective(log2);
    }

    public void updateGx() {
        CredentialsLog credentialsLog = credentialsLogMapper.selectSum();
        List<Subjects> list = subjectsMapper.selectByExample(null);
        int a=0,b=0,c=0;
        for (Subjects s:list) {
            if(s.getQcBorrowerNumber()>0){
                if (s.getQcBorrowerBalance()>0){
                    a+=s.getQcBorrowerNumber()*s.getQcBorrowerBalance();
                }
            }
            if(s.getQcLenderNumber()>0){
                if (s.getQcLenderBalance()>0){
                    b+=s.getQcLenderNumber()*s.getQcLenderBalance();
                }
            }
        }
        a+=Integer.parseInt(credentialsLog.getJzJfprice());
        b+=Integer.parseInt(credentialsLog.getJzDfprice());
        c=a-b;
        financialMapper.update1(a,Integer.parseInt(credentialsLog.getJzJfprice()));
        financialMapper.update2(b,Integer.parseInt(credentialsLog.getJzDfprice()));
        financialMapper.update3(c);
    }

}
